#include<iostream>
#include<cstring>
using namespace std;
const int N = 6e3 + 10;
int n, w[N];
int st[N];
int f[N][2];
int h[N], e[N], ne[N], idx;
void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int u)
{
	f[u][1] = w[u];
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		dfs(j);
		f[u][1] += f[j][0];
		f[u][0] += max(f[j][0], f[j][1]);
	}
}
int main()
{
	cin >> n;
	memset(h, -1, sizeof(h));
	for (int i = 1; i <= n; i++)cin >> w[i];
	for (int i = 1; i <= n - 1; i++) {
		int a, b;
		cin >> a >> b;
		add(a, b);
		st[a] = b;
	}
	int root = 1;
	while (st[root])root = st[root];
	dfs(root);
	cout << max(f[root][0], f[root][1]) << endl;
	return 0;
}